package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.AdminDTO;
import com.quanquan.management.request.AdminRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;
import utils.StringUtil;

/**
 * @author gongzhou
 * @title: ModuleService
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/3/289:33
 */
@Service
public class AdminService {

    @Value("${module-url.storeUrl}")
    private String storeUrl;

    public PageDTO<AdminDTO> list(AdminRequest requestBody) {
        String json = Request.postJson(storeUrl + "admin/query_list_manage", requestBody);
        PageDTO<AdminDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            return data;
        }
        return pageDTO;
    }

    public boolean delete(String id) {
        AdminRequest requestBody = new AdminRequest();
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "admin/remove_banner", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return resCode == 0;
    }

    public AdminDTO findById(String id) {
        AdminRequest requestBody = new AdminRequest();
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "admin/find_by_id", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        AdminDTO adminDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            adminDTO = data.toJavaObject(AdminDTO.class);
        }
        return adminDTO;
    }

    public boolean add(AdminRequest requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "admin/add_admin", requestBody);
        if (StringUtil.isEmpty(json)) {
            return false;
        }
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return Integer.valueOf(0).equals(resCode);
    }

    public boolean update(AdminRequest requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "admin/update", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean findByAccount(String account){
        AdminRequest adminRequest = new AdminRequest();
        adminRequest.setAccount(account);
        String json = Request.postJson(storeUrl + "admin/find_by_account",adminRequest);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

}
